<%@page language="java"
	import="java.sql.PreparedStatement,java.sql.ResultSet"
	pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>会员信息修改</title>
<style>
.main {
	width: 800px;
	height: 300px;
	border: 1px red solid;
	margin: 0px auto;
}

form {
	margin-top: 70px;
	margin-left: 240px;
	line-height: 30px;
}
</style>
</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="conn.jsp"%>
	<!-- 得到连接对象 -->
	<%
    	request.setCharacterEncoding("utf8");  //设置表单请求编码，防止中文乱码
		String un=(String)session.getAttribute("username");
		String sql;	PreparedStatement pst;	ResultSet rs;
		
    	if(null==un){
    		//out.print(un);  //测试
    		out.print("<script>alert('请先登录!');location.href='index.jsp'</script>");
    	}else{
			sql="select * from user where username=?";  //参数式查询
			pst = conn.prepareStatement(sql);  
			pst.setString(1,un);  
			rs = pst.executeQuery();  //预编译，带缓冲的选择查询
			rs.next();  //定位到指定的记录
			%>
	<div class="main">
		<form method="post">
			会员密码：<input type="password" name="password"
				value=<%=rs.getString("password")%>> <br /> 会员真名：<input
				type="text" name="realname" value=<%=rs.getString("realname")%>>
			<br /> 手机号码：<input type="text" name="mobile"
				value=<%=rs.getString("mobile")%>> <br /> 会员年龄：<input
				type="text" name="age" value=<%=rs.getString("age")%>> <br />
			<input type="submit" value="提交" name="btn" />
		</form>
	</div>
	<%}%>

	<%
		String btn = request.getParameter("btn");
		//out.print(btn);  //未提交前值为null，提交后值为“提交”
		if(btn != null && btn.equals("提交")){
 			String pw=request.getParameter("password");
			String rn=request.getParameter("realname");
			String tel=request.getParameter("mobile");
			int age =Integer.valueOf(request.getParameter("age"));
			//写入数据库user表
			sql="update user set password=?,realname=?,mobile=?,age=? where username=?";  //参数式查询
			pst = conn.prepareStatement(sql);  
			pst.setString(1,pw); 
			pst.setString(2,rn);  
			pst.setString(3,tel); 
			pst.setInt(4,age); 
			pst.setString(5,un);
			pst.executeUpdate();  //预编译，带缓冲的操作查询
			conn.close();
			out.print("<script>alert('信息修改成功!');location.href='index.jsp'</script>"); 
		}
	 %>
	<%@ include file="footer.jsp"%>
</body>
</html>